package com.cheetah.support.redis;

import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author Administrator
 * @Date 2019/9/19
 * @Version V1.0
 * @Description:
 **/
public class RedisClient {

    private RedisTemplate redisTemplate;

    private ApplicationContext context;

    public RedisClient(RedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }

    public void set(String key, Object value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void set(String key,String value,long timeout){
        redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }

    public String get(String key){
        return (String)redisTemplate.opsForValue().get(key);
    }

    public void hSet(String key,String field,Object value){
        redisTemplate.opsForHash().put(key,field,value);
    }

    public String hGet(String key,String field){
        return (String)redisTemplate.opsForHash().get(key,field);
    }

    public List<String> hGetAll(String key){
        return (List<String>)redisTemplate.opsForHash().values(key);
    }

    public long lPush(String key,Object value){
        return redisTemplate.opsForList().leftPush(key,value);
    }

    public long rPush(String key,Object value){
        return redisTemplate.opsForList().rightPush(key,value);
    }

    public long lPushAll(String key,Object ... values){
        return redisTemplate.opsForList().leftPushAll(key,values);
    }

    public void lPop(String key){
        redisTemplate.opsForList().leftPop(key);
    }

    public void rPop(String key){
        redisTemplate.opsForList().rightPop(key);
    }

    public long rPushAll(String key,Object ... values){
        return redisTemplate.opsForList().rightPushAll(key,values);
    }

    public long sAdd(String key,Object ... values){
        return redisTemplate.opsForSet().add(key,values);
    }

    public Set<String> sMembers(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public boolean sIsMember(String key,Object o){
        return redisTemplate.opsForSet().isMember(key,o);
    }

    public boolean zSet(String key,String value,long score){
        return redisTemplate.opsForZSet().add(key,value,score);
    }

    public Long zSet(String key, Set<ZSetOperations.TypedTuple> tuple){
        return redisTemplate.opsForZSet().add(key,tuple);
    }
}
